package exam.axhu.junit;

import static org.junit.Assert.*;

import java.util.List;

import exam.axhu.entity.Exam;
import exam.axhu.entity.ExamPaper;
import exam.axhu.entity.TestPaper;
import exam.axhu.service.ExamService;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ExamTest {
	private static ExamService examService;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		try {
			ApplicationContext act = new ClassPathXmlApplicationContext(
					"beans.xml");
			examService = (ExamService) act.getBean("examServiceImpl");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	public void addexam() {
		examService.addExam(new Exam("Test101", "CS101", "计算机导论第一次考试"));
		examService.addExam(new Exam("Test102", "CS102", "计算机导论第二次考试"));
		examService.addExam(new Exam("Test104", "CS101", "计算机导论第si次考试"));

	}

	@Test
	public void deleteexam() {
		examService.deleteExam("Test101");
	//	examService.deleteExam("C102");
	//	examService.deleteExam("C104");		
	}

	@Test
	public void findexam() {
		if (examService.findByExamID("Test102") != null)
			System.out.println(examService.findByExamID("Test102")
					.getName());
		else
			System.out.println("NO");
	}

	@Test
	public void listexam() {
		List<Exam> ems = examService.findByCourseID("CS101");
		if (ems != null)
			for (Exam em : ems)
				System.out.println(em.getExamID());
		else
			System.out.println("NO");
	}

	@Test
	public void updateexam(){
		Exam em = examService.findByExamID("Test102");
		em.setName("ComputerScience first test");
		examService.updateExam(em);
	}
	
	@Test
	public void setstatus(){
		Exam em = examService.findByExamID("Test102");
		examService.setStatus(em, 1);
	}
	
// ExamPaper	
	@Test
	public void addexampaper() {
		examService.addExamPaper("Test101", "Choice", "22");
		examService.addExamPaper("Test101", "Choice", "24");
		examService.addExamPaper("Test101", "ShortAnswer", "25");

	}

	@Test
	public void deleteexampaper() {
		//examService.deleteExamPaper("Test101", "choice", "12");
		examService.deleteWholeExamPaper("Test101");
	//	examService.deleteExam("C104");		
	}

	@Test
	public void updateexampaper(){
		ExamPaper em = examService.searchExamPaper("Test101", "choice", "14");
		em.setItemID("114");
		examService.updateExamPaper(em);
	}
	
	@Test
	public void findexampaper() {
		ExamPaper em = examService.searchExamPaper("Test101", "choice", "14");
		System.out.println(em.getExamPaperID());
		
	}

	@Test
	public void listexampaper() {
		//List<ExamPaper> ems = examService.listExamPaperByExamID("Test101");
		List<ExamPaper> ems = examService.listExamPaperByJudgeItem("Test101");
		if (ems != null)
			for (ExamPaper em : ems)
				System.out.println(em.getExamPaperID());
		else
			System.out.println("NO");
	}

	
	// TestPaper	
		@Test
		public void addtestpaper() {
			examService.addTestPaper("Test101choice15", "S10001", "C");
			examService.addTestPaper("Test101choice14", "S10002", "B");
			examService.addTestPaper("Test101choice14", "S10003", "C");

		}

		@Test
		public void deletetestpaper() {
			//examService.deleteTestPaper("Test101choice14", "S10001", "D");
			examService.deleteWholeTestPaper("Test101choice14");
		//	examService.deleteExam("C104");		
		}

		@Test
		public void updatetestpaper(){
			TestPaper em = examService.searchTestPaper("Test101choice14", "S10003", "C");
			em.setAnswer("F");
			examService.updateTestPaper(em);
		}
		
		@Test
		public void findtestpaper() {
			TestPaper em = examService.searchTestPaper("Test101choice14", "S10003", "C");
			System.out.println(em.getAnswer());
			
		}

		@Test
		public void listtestpaper() {
			List<TestPaper> ems = examService.listTestPaperByExamPaperID("Test101choice14");
			if (ems != null)
				for (TestPaper em : ems)
					System.out.println(em.getStudentID());
			else
				System.out.println("NO");
		}
		
		@Test
		public void listbystudentID(){
			List<TestPaper> ems = examService.listTestPaperByStudentID("S10001");
			if (ems != null)
				for (TestPaper em : ems)
					System.out.println(em.getAnswer());
			else
				System.out.println("NO");
		}
		
		@Test
		public void listbycourseIDandstatus(){
			List<Exam> ems = examService.listExamsByCourseAndStatus("CS102",1);
			if (ems != null)
				for (Exam em : ems)
					System.out.println(em.getExamID());
			else
				System.out.println("NO");
		}
		
}
